﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLocalChurch.Business;
using MyLocalChurch.Resources;

namespace MyLocalChurch.WebControls
{
    public partial class DatePicker : UserControl
    {
        public event EventHandler SelectedDateChanged;

        public bool Required
        {
            get { return (bool)ViewState["DatePicker_Required"]; }
            set { ViewState["DatePicker_Required"] = value; }
        }

        public bool IsValid
        {
            get { return (bool) ViewState["DatePicker_IsValid"]; }
            set { ViewState["DatePicker_IsValid"] = value; }
        }

        public DateTime? MinDate
        {
            get { return ViewState["DatePicker_MinDate"] as DateTime?; }
            set { ViewState["DatePicker_MinDate"] = value; }
        }

        public DateTime? MaxDate
        {
            get { return ViewState["DatePicker_MaxDate"] as DateTime?; }
            set { ViewState["DatePicker_MaxDate"] = value; }
        }

        public DateTime? SelectedDate
        {
            get { return ViewState["DatePicker_SelectedDate"] as DateTime?; }
            set { ViewState["DatePicker_SelectedDate"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (SelectedDate.HasValue)
                    txtDate.Text = SelectedDate.Value.ToString("d");

                return;
            }

            SelectedDate = StringParser.ParseDateOrNull(txtDate.Text);
        }

        protected void cvalDate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (!SelectedDate.HasValue)
            {
                if (!Required)
                    return;

                args.IsValid = false;
                IsValid = false;
                cvalDate.ErrorMessage = ValidationMessages.Required;
                return;
            }

            if (MinDate.HasValue && SelectedDate < MinDate)
            {
                args.IsValid = false;
                IsValid = false;
                cvalDate.ErrorMessage = string.Format(ValidationMessages.ErrorMinDate, MinDate.Value.ToString("d"));
                return;
            }

            if (MaxDate.HasValue && SelectedDate > MaxDate)
            {
                args.IsValid = false;
                IsValid = false;
                cvalDate.ErrorMessage = string.Format(ValidationMessages.ErrorMaxDate, MaxDate.Value.ToString("d"));
            }
        }

        protected void txtDate_TextChanged(object sender, EventArgs e)
        {
            OnSelectedDateChanged();
        }

        protected virtual void OnSelectedDateChanged()
        {
            if (SelectedDateChanged != null)
                SelectedDateChanged(this, EventArgs.Empty);
        }
    }
}